package com.eastfair.auth.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eastfair.auth.dto.HuaWeiAccountDTO;
import com.eastfair.auth.enumeration.IamSystemSceneEnum;
import com.eastfair.auth.enumeration.OrganizationOrgTypeEnum;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.httpClient.GetHttpClient;
import com.eastfair.core.httpClient.PostHttpClient;
import com.eastfair.core.utils.HttpClientUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class HuaWeiIamUtil {

    @Value("${com.iam.host}")
//    private String host = "iam.hi.itgexpo.com";
    private String host;

    @Value("${com.iam.port}")
    private Integer port;

    @Value("${com.iam.p_venue_clientSecret}")
    private String p_venue_clientSecret;

    @Value("${com.iam.p_certificate_clientSecret}")
    private String p_certificate_clientSecret;

    @Value("${com.iam.p_composite_venue_clientSecret}")
    private String p_composite_venue_clientSecret;


    @Value("${com.iam.p_pay_clientSecret}")
    private String p_pay_clientSecret;



    @Value("${com.iam.p_visitor_clientSecret}")
    private String p_visitor_clientSecret;

    @Value("${com.iam.p_clientSecret}")
    private String p_clientSecret;

    /**
     * client_id：PVENUE
     * client_secret：0659711663724104812335545c15a342
     * 认证服务地址：https://iam.hi.itgexpo.com:8443/idp
     * <p>
     * 10.11.80.243 iam.hi.itgexpo.com
     */
    /**
     * 获取token
     *
     * @param clientId
     * @param code
     * @return
     */
    public String getToken(String clientId, String code) {

//        String url = String.format("https://%s:%s/idp/oauth2/getToken?client_id=%s&grant_type=authorization_code&code=%s&client_secret=%s",
//                host, port, clientId, code, clientSecret);
        String clientSecret = "";

        if (OrganizationOrgTypeEnum.P_VENUE.getCode().equals(ContextUtil.getSystemScene())) {
            clientSecret = p_venue_clientSecret;
            clientId = IamSystemSceneEnum.PVENUE.getCode();
        } else if (OrganizationOrgTypeEnum.P_CERTIFICATE.getCode().equals(ContextUtil.getSystemScene())) {
            clientSecret = p_certificate_clientSecret;
            clientId = IamSystemSceneEnum.PCERTIFI.getCode();
        } else if (OrganizationOrgTypeEnum.P_COMPOSITE_VENUE.getCode().equals(ContextUtil.getSystemScene())) {
            clientSecret = p_composite_venue_clientSecret;
            clientId = IamSystemSceneEnum.PCOMVENUE.getCode();
        } else if (OrganizationOrgTypeEnum.P_PAY.getCode().equals(ContextUtil.getSystemScene())) {
            clientSecret = p_pay_clientSecret;
            clientId = IamSystemSceneEnum.PPAY.getCode();
        } else if (OrganizationOrgTypeEnum.P_VISITOR_BACK.getCode().equals(ContextUtil.getSystemScene())) {
            clientSecret = p_visitor_clientSecret;
            clientId = IamSystemSceneEnum.PVISITOR.getCode();
        }else if (OrganizationOrgTypeEnum.P.getCode().equals(ContextUtil.getSystemScene())) {
            clientSecret = p_clientSecret;
            clientId = IamSystemSceneEnum.P.getCode();
        }




        String url = String.format("https://%s:%s/idp/oauth2/getToken?code=%s&grant_type=%s&client_secret=%s&client_id=%s",
                host, port, code, "authorization_code", clientSecret, clientId);
//        Map<String, String> params = new HashMap<>();
//        params.put("client_id", clientId);
//        params.put("grant_type", "authorization_code");
//        params.put("code", code);
//        params.put("client_secret", clientSecret);

        JSONObject params = new JSONObject();
        params.put("client_id", clientId);
        params.put("grant_type", "authorization_code");
        params.put("code", code);
        params.put("client_secret", clientSecret);
        log.info("getToken systemScene is {}", ContextUtil.getSystemScene());
        log.info("getToken url is {}", url);
        log.info("getToken params is {}", JSONObject.toJSONString(params));
        String msg = PostHttpClient.postForm(url, params);
        try {
            log.info("getToken msg is {}", msg);
            if (StringUtils.isNotBlank(msg)) {
                JSONObject jsonObject = JSONObject.parseObject(msg);
                return jsonObject.getString("access_token");
            }
        } catch (Exception e) {
            return null;
        }
        return null;
    }


    public HuaWeiAccountDTO getUser(String clientId, String token) {
        if (OrganizationOrgTypeEnum.P_VENUE.getCode().equals(ContextUtil.getSystemScene())) {
            clientId = IamSystemSceneEnum.PVENUE.getCode();
        } else if (OrganizationOrgTypeEnum.P_CERTIFICATE.getCode().equals(ContextUtil.getSystemScene())) {
            clientId = IamSystemSceneEnum.PCERTIFI.getCode();
        } else if (OrganizationOrgTypeEnum.P_COMPOSITE_VENUE.getCode().equals(ContextUtil.getSystemScene())) {
            clientId = IamSystemSceneEnum.PCOMVENUE.getCode();
        }else if (OrganizationOrgTypeEnum.P_PAY.getCode().equals(ContextUtil.getSystemScene())) {
            clientId = IamSystemSceneEnum.PPAY.getCode();
        }else if (OrganizationOrgTypeEnum.P_VISITOR_BACK.getCode().equals(ContextUtil.getSystemScene())) {
            clientId = IamSystemSceneEnum.PVISITOR.getCode();
        }else if (OrganizationOrgTypeEnum.P.getCode().equals(ContextUtil.getSystemScene())) {
            clientId = IamSystemSceneEnum.P.getCode();
        }

        String url = String.format("https://%s:%s/idp/oauth2/getUserInfo?access_token=%s&client_id=%s", host, port, token, clientId);
//        url = "https://iam.hi.itgexpo.com:8443/idp/oauth2/getUserInfo?access_token=9abd6a148ba2dabdea483330b07ec033&client_id=PVENUE";
        log.info("getUser url is {}", url);
        String msg = GetHttpClient.get(url, null);
        log.info("getUser msg is {}", msg);
        try {
            if (StringUtils.isNotBlank(msg)) {
                JSONObject jsonObject = JSONObject.parseObject(msg);
                HuaWeiAccountDTO huaWeiAccountDTO = new HuaWeiAccountDTO();
                JSONArray jsonArray = jsonObject.getJSONArray("spRoleList");
                if (jsonArray != null && !jsonArray.isEmpty()) {
                    huaWeiAccountDTO.setLoginName(jsonArray.getString(0));
                }
                log.info("huaWeiAccountDTO is {}", JSONObject.toJSONString(huaWeiAccountDTO));
                return huaWeiAccountDTO;
            }
        } catch (Exception e) {
            return null;
        }
        return null;
    }


    public static void main(String[] args) {


//        https://iam.hi.itgexpo.com:8443/idp/oauth2/getToken?code=11cbef7cbaa210a2a987dd103562239c&grant_type=authorization_code&client_secret=0659711663724104812335545c15a342&client_id=PVENUE


        String token = new HuaWeiIamUtil().getToken("PVENUE", "08f3ff40b66d4164f05b67b550af99f3");
        System.out.println(token);


//        HuaWeiAccountDTO huaWeiAccountDTO = getUser("iam.hi.itgexpo.com", 8443, "PVENUE", "9abd6a148ba2dabdea483330b07ec033");
//        System.out.println(JSONObject.toJSONString(huaWeiAccountDTO));
    }


//    public String assmbleSysScene(String sysScene) {
//
////        String url = String.format("https://%s:%s/idp/oauth2/getToken?client_id=%s&grant_type=authorization_code&code=%s&client_secret=%s",
////                host, port, clientId, code, clientSecret);
//
//        String url = String.format("https://%s:%s/idp/oauth2/getToken", host, port);
//        Map<String, String> params = new HashMap<>();
//        params.put("client_id", clientId);
//        params.put("grant_type", "authorization_code");
//        params.put("code", code);
//        params.put("client_secret", clientSecret);
//        log.info("url is {}", url);
//        String msg = PostHttpClient.doPostForm(url, params);
//        try {
//            if (StringUtils.isNotBlank(msg)) {
//                JSONObject jsonObject = JSONObject.parseObject(msg);
//                return jsonObject.getString("access_token");
//            }
//        } catch (Exception e) {
//            return null;
//        }
//        return null;
//    }


}
